# coding: utf-8

#-------------------------------------------------------------------------------------------------------------------------------------
#This Function can create stretch node with ikhandle(note: don's support ikspline !)
#Note: can create stretch node
#FN: ikStretchNode(prefix)
#Date: 2012/10/31_v1.0

import maya.cmds as mc
import RosaCommands as rc

def ikStretchNode( prefix ,type , vectorctrl=None):

        #used stretch maya node.
        glStrechMd = mc.createNode('multiplyDivide' , n = prefix+'_stretchPro_md')
        glStrechCon = mc.createNode('condition' , n = prefix+'_stretch_con')
        glStrechOnOffBd = mc.createNode('blendColors' , n = prefix+'_stretchOnOff_bd')
        glStrechOutPutMd = mc.createNode('multiplyDivide' , n = prefix+'_stretchOutputValue_md')
        glscaleMd = mc.createNode('multiplyDivide' , n = prefix+'_globalScale_stretch_md')
        glCurrentpma = mc.createNode('plusMinusAverage' , n = prefix+'_currentDisPlus_pma')
        glRealDisDB = mc.createNode('distanceBetween' , n = prefix+'_realDis_db')
        startpointLoc = mc.spaceLocator(p=(0,0,0) , n = prefix+'_startpoint_loc' )
        endpointLoc = mc.spaceLocator(p=(0,0,0) , n = prefix+'_endpoint_loc' )

        changeLeftorRightMD = mc.createNode('multiplyDivide' , n = prefix+'_change_LeftorRight_md')

        #used jointLock maya node.
        if type == 2:

                lock01disbetween = mc.createNode('distanceBetween' , n = prefix+'_lock01disbetween_pma')
                lock02disbetween = mc.createNode('distanceBetween' , n = prefix+'_lock02disbetween_pma')

                lockOnOffNode = mc.createNode('multiplyDivide' , n = prefix+'_lockOnOff_md')
                LockStretchChange = mc.createNode('multiplyDivide' , n = prefix+'_LockStretchChange_md')
                reverseNode = mc.createNode('remapValue' , n = prefix+'_reverseLockValue_rv')

                lockglscaleNode = mc.createNode( 'multiplyDivide' , n = prefix+'_lockGlscale_md')

                finalOutputValue = mc.createNode('plusMinusAverage' , n = prefix+'_finalOutputValue_pma')
        else:
                pass



        #used command bridge.
        cmdgrp  = mc.createNode('transform' ,  n = prefix+'_StretchCmd_Bridge')
        rc.addNewAttr(cmdgrp , 1 , 0.01 , 100 , 'globalScale')
        rc.addNewAttr(cmdgrp , 0 , 0 , 1 , 'stretch' )
        if type == 2:
                rc.addNewAttr(cmdgrp , 0 , 0 , 1 , 'Elbow_Or_Knee_lock' , '_')
        elif type ==3:
                rc.addNewAttr(cmdgrp , 0 , 0 , 1 ,  '_')
        rc.addNewAttr(cmdgrp , 1 , -1000 , 1000 ,  'realDis' , 'gl_currentDis' , '__' , 'org_first_jointDis' , 'org_secound_jointDis' , 'org_thirty_jointDis' , '___' ,'real_first_jointDis' , 'real_secound_jointDis' , 'real_thirty_jointDis')

        rc.lockhideAttr(cmdgrp , 0 ,  'translateX' , 'translateY' , 'translateZ' , 'rotateX' , 'rotateY' , 'rotateZ' , 'scaleX' , 'scaleY' , 'scaleZ' , 'visibility' )
        rc.lockhideAttr(cmdgrp , 1 , '_' , '__', '___',)
        #connect attribute.
#---------------------------------------------------------------------------------------------------------------------


        if type == 2:
                mc.connectAttr( cmdgrp+'.org_first_jointDis' , glCurrentpma+'.input1D[0]' )
                mc.connectAttr( cmdgrp+'.org_secound_jointDis' , glCurrentpma+'.input1D[1]' )
        elif type == 3:
                mc.connectAttr( cmdgrp+'.org_first_jointDis' , glCurrentpma+'.input1D[0]' )
                mc.connectAttr( cmdgrp+'.org_secound_jointDis' , glCurrentpma+'.input1D[1]' )
                mc.connectAttr( cmdgrp+'.org_thirty_jointDis' , glCurrentpma+'.input1D[2]' )


        mc.connectAttr( glCurrentpma+'.output1D ', changeLeftorRightMD+'.input2.input2X' )

        # I will solve the right stretch bad question !
        ########################################
        if 'left' in prefix or 'Left' in prefix:
                mc.setAttr(changeLeftorRightMD+'.input1.input1X' , 1)
        elif 'right' in prefix or 'Right' in prefix:
                mc.setAttr(changeLeftorRightMD+'.input1.input1X' , -1)
        #########################################

        mc.connectAttr( changeLeftorRightMD+'.output.outputX' , glscaleMd+'.input2.input2X' )
        mc.connectAttr( cmdgrp+'.globalScale' , glscaleMd+'.input1.input1X' )
        mc.connectAttr( glscaleMd+'.output.outputX' ,  cmdgrp+'.gl_currentDis')

        mc.connectAttr( startpointLoc[0]+'Shape.worldPosition[0]' ,  glRealDisDB+'.point1' )
        mc.connectAttr( endpointLoc[0]+'Shape.worldPosition[0]' ,  glRealDisDB+'.point2' )
        mc.connectAttr( glRealDisDB+'.distance' , cmdgrp+'.realDis')
 #---------------------------------------------------------------------------------------------------------------------

        mc.setAttr( glStrechMd+'.operation' , 2 )
        mc.connectAttr( cmdgrp+'.realDis' , glStrechMd+'.input1.input1X' )
        mc.connectAttr( cmdgrp+'.gl_currentDis' , glStrechMd+'.input2.input2X')
 #---------------------------------------------------------------------------------------------------------------------

        mc.connectAttr( cmdgrp+'.realDis' , glStrechCon+'.firstTerm' )
        mc.connectAttr( cmdgrp+'.gl_currentDis' , glStrechCon+'.secondTerm' )
        mc.setAttr ( glStrechCon+'.operation' , 3)
        mc.connectAttr( glStrechMd+'.output.outputX' , glStrechCon+'.colorIfTrue.colorIfTrueR' )
        mc.setAttr ( glStrechCon+'.colorIfTrueG' , 1)

        mc.connectAttr( glStrechCon+'.outColor.outColorR' , glStrechOnOffBd+'.color1.color1R' )
        mc.connectAttr( glStrechCon+'.outColor.outColorG' , glStrechOnOffBd+'.color2.color2R' )
        mc.connectAttr( cmdgrp+'.stretch' , glStrechOnOffBd+'.blender' )
#---------------------------------------------------------------------------------------------------------------------
        mc.connectAttr( cmdgrp+'.org_first_jointDis' , glStrechOutPutMd+'.input1.input1X' )
        mc.connectAttr( cmdgrp+'.org_secound_jointDis' , glStrechOutPutMd+'.input1.input1Y' )
        mc.connectAttr( cmdgrp+'.org_thirty_jointDis' , glStrechOutPutMd+'.input1.input1Z' )

        mc.connectAttr( glStrechOnOffBd+'.output.outputR' , glStrechOutPutMd+'.input2.input2X' )
        mc.connectAttr( glStrechOnOffBd+'.output.outputR' , glStrechOutPutMd+'.input2.input2Y' )
        mc.connectAttr( glStrechOnOffBd+'.output.outputR' , glStrechOutPutMd+'.input2.input2Z' )
 #---------------------------------------------------------------------------------------------------------------------

        if type == 2:
                mc.connectAttr( vectorctrl+'Shape.worldPosition[0]' , lock01disbetween+'.point2')
                mc.connectAttr( vectorctrl+'Shape.worldPosition[0]' , lock02disbetween+'.point2')
                mc.connectAttr( startpointLoc[0]+'Shape.worldPosition[0]' ,  lock01disbetween+'.point1' )
                mc.connectAttr( endpointLoc[0]+'Shape.worldPosition[0]' ,  lock02disbetween+'.point1' )

                mc.setAttr( lockglscaleNode+'.operation' , 2)
                mc.connectAttr( lock01disbetween+'.distance' , lockglscaleNode+'.input1X')
                mc.connectAttr( lock02disbetween+'.distance' , lockglscaleNode+'.input1Y')
                mc.connectAttr( cmdgrp+'.globalScale' , lockglscaleNode+'.input2X')
                mc.connectAttr( cmdgrp+'.globalScale' , lockglscaleNode+'.input2Y')

                mc.connectAttr( lockglscaleNode+'.outputX' , lockOnOffNode+'.input1X')
                mc.connectAttr( lockglscaleNode+'.outputY' , lockOnOffNode+'.input1Y')

                mc.connectAttr( cmdgrp+'.Elbow_Or_Knee_lock' , lockOnOffNode+'.input2X')
                mc.connectAttr( cmdgrp+'.Elbow_Or_Knee_lock' , lockOnOffNode+'.input2Y')

                mc.connectAttr( lockOnOffNode+'.outputX' , finalOutputValue+'.input3D[0].input3Dx')
                mc.connectAttr( lockOnOffNode+'.outputY' , finalOutputValue+'.input3D[0].input3Dy')

                mc.connectAttr( cmdgrp+'.Elbow_Or_Knee_lock' , reverseNode+'.inputValue')
                mc.setAttr( reverseNode+'.outputMin' , 1)
                mc.setAttr( reverseNode+'.outputMax' , 0)
                mc.connectAttr( reverseNode+'.outValue' ,  LockStretchChange+'.input1X')
                mc.connectAttr( reverseNode+'.outValue' ,  LockStretchChange+'.input1Y')

                mc.connectAttr( glStrechOutPutMd+'.outputX' , LockStretchChange+'.input2X')
                mc.connectAttr( glStrechOutPutMd+'.outputY' , LockStretchChange+'.input2Y')

                mc.connectAttr( LockStretchChange+'.outputX' , finalOutputValue+'.input3D[1].input3Dx')
                mc.connectAttr( LockStretchChange+'.outputY' , finalOutputValue+'.input3D[1].input3Dy')

                mc.connectAttr( finalOutputValue+'.output3Dx' , cmdgrp+'.real_first_jointDis' )
                mc.connectAttr( finalOutputValue+'.output3Dy' , cmdgrp+'.real_secound_jointDis' )
        else :
                mc.connectAttr( glStrechOutPutMd+'.outputX' , cmdgrp+'.real_first_jointDis' )
                mc.connectAttr( glStrechOutPutMd+'.outputY' , cmdgrp+'.real_secound_jointDis' )
                mc.connectAttr( glStrechOutPutMd+'.outputZ' , cmdgrp+'.real_thirty_jointDis' )
  #---------------------------------------------------------------------------------------------------------------------

        return cmdgrp  , startpointLoc[0] , endpointLoc[0] , cmdgrp , glStrechMd

